package com.ruoyi.jihuo.api.official;

import com.ruoyi.jihuo.api.ApiStrategy;
import com.ruoyi.jihuo.domain.JhTmAccount;
import com.taobao.top.BaseTopApiRequest;
import com.taobao.top.BaseTopApiResponse;
import com.taobao.top.TopApiClient;
import com.taobao.top.defaultability.Defaultability;
import com.taobao.top.defaultability.request.TaobaoItemsOnsaleGetRequest;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

@Component
public class ItemsOnsaleGetStrategy implements ApiStrategy {

    @Override
    public BaseTopApiResponse execute(TopApiClient client, BaseTopApiRequest request, JhTmAccount jhTmAccount) throws IOException {
        Defaultability apiPackage = new Defaultability(client);
        return apiPackage.taobaoItemsOnsaleGet((TaobaoItemsOnsaleGetRequest) request, jhTmAccount.getJhAppSession());
    }

    @Override
    public BaseTopApiRequest setParams(String api, Map<String, Object> paramMap) {
        TaobaoItemsOnsaleGetRequest request = new TaobaoItemsOnsaleGetRequest();
        String fields = (String) paramMap.get("fields");
        Long pageNo = (Long) paramMap.get("page_no");
        Long pageSize = (Long) paramMap.get("page_size");
        request.setPageNo(pageNo);
        request.setPageSize(pageSize);
        request.setFields(Arrays.asList(fields.split(",")));
        request.setOrderBy((String) paramMap.get("order_by"));
        return request;
    }
}
